<script setup lang="ts">
import { ref, computed, unref, h, toRaw, reactive, type PropType, watch } from 'vue';
import { Modal, Timeline, TimelineItem, Button, Spin } from 'ant-design-vue';
import type { Recordable } from "@vben/types";
import { isFunction } from "@/utils/is";
import { ywApprovalBack, ywApprovalPass, ywApprovalShow } from '@/api/sys/approval';
import PassModal from './PassModal.vue';
import { useModal } from "@/components/Modal";
import { useMessage } from "@/hooks/web/useMessage";
import { useLocaleStore } from '@/store/modules/locale';
import { useGo } from '@/hooks/web/usePage';


const emit = defineEmits(['success']);
const showBtnData = ref({});
const localeStore = useLocaleStore();
const go = useGo();
const loading = ref(false);
const isFirstLoaded = ref<Boolean>(false);
const dataList = ref([]);
const [register, { openModal, closeModal }] = useModal();
const { createMessage } = useMessage();

const showToast = ref(false);//是否对这些单据进行重新发起

defineOptions({ name: 'approverLogView' });
const props = defineProps({
  approveNo: { type: String, default: "" },
  api: { type: Function as PropType<(arg?: any) => Promise<Recordable<any>>> },
  passApi: { type: Function as PropType<(arg?: any) => Promise<Recordable<any>>> },
  backApi: { type: Function as PropType<(arg?: any) => Promise<Recordable<any>>> },
  afterFetch: {
    type: Function as PropType<Fn>,
    default: null,
  },
  list:{type:Array as PropType<Recordable<any>>} ,
});



watch(
  () => props.approveNo,
  () => {
    ywApprovalShowFn();
  },
  { deep: true },
);

function ywApprovalShowFn() {
  loading.value = true;

  ywApprovalShow({ approveNo: props.approveNo }).then(res => {
    if (res.code == 200) {
      loading.value = false;
      isFirstLoaded.value = true;

      showBtnData.value = res.data;
    }
  })
}

function passFn() {
  openModal(true, {
    type: "通过",
  });
}

function backFn() {
  openModal(true, {
    type: "驳回",
  });
}

function reloadFn() {
  ywApprovalShowFn();
}
async function handleSuccess(data) {
  closeModal();
  emit('success');
  ywApprovalShowFn();
}
const order_nos=ref([]);
const delete_order_nos=ref([]);
function againFn(){
  let arr= props.list;
  //已撤销  已驳回
  let order_no_arr=[];
  arr.forEach(item => {
    item.orderNo=item.orderNo.replace("已撤销","");
    item.orderNo=item.orderNo.replace("已驳回","");
    order_no_arr.push({no:item.orderNo,show:true});
  })
  order_nos.value=order_no_arr;
  showToast.value=true;

}
function againSubFn(){
  // console.log(order_nos.value);
  // debugger
  let arr= [];
  order_nos.value.forEach(item => {
    if(item.show){
      arr.push(item.no);
    }
  })
  if(arr.length>0){
    localeStore.setWaitList(arr);
    go({
      path: `/yw/apply/ywMoneyList2`, query: { isAgain:"1" }
    })
  }

}

function deleteItem(index){
  // let arr=order_nos.value;
  // arr.splice(index, 1)
  // order_nos.value=arr;

  order_nos.value[index].show=false;

}

defineExpose({
  reloadFn,
})
</script>

<template>


  <Button type="primary" v-if="showBtnData?.currentApprovalShow" @click="passFn">审批通过</Button>
  <Button danger v-if="showBtnData?.currentApprovalShow" @click="backFn">审批驳回</Button>
  <Button type="primary" v-if="showBtnData?.canAgainCreateShow" @click="againFn">重新发起</Button>

  <PassModal @register="register" @success="handleSuccess" :approveNo="props.approveNo" :passApi="props.passApi"
    :back-api="props.backApi"></PassModal>
  <Modal v-model:visible="showToast" title="是否对这些单据进行重新发起" @ok="againSubFn">
    <div v-for="(item, index) in order_nos">
      <p v-if="item.show">{{ item.no }} <vxe-button style="padding-left: 6px;" mode="text" status="error" content="删除" @click="deleteItem(index)"></vxe-button></p>
    </div>
  </Modal>
</template>

<style scoped lang="less">
.line-time {
  color: gray;
  font-size: 12px;
}

.line-item {
  margin-left: 3px;
}
</style>
